package com.linsen.core.common.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.linsen.core.common.constant.AuthConstant;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 认证授权工具类
 *
 * @author zhaoyong
 * @date 2023/3/13
 */
public final class SecurityUtil {
    public final static String[] TOKEN_NAME_HEADERS = new String[]{AuthConstant.TOKEN_NAME};

    /**
     * 请求头获取Token
     *
     * @param request
     * @return
     */
    public static String getHeaderToken(HttpServletRequest request) {
        for (String tokenName : TOKEN_NAME_HEADERS) {
            String tokenValue = request.getHeader(tokenName);
            if (StrUtil.isBlank(tokenValue)) {
                continue;
            }

            return tokenValue;
        }

        return null;
    }

    /**
     * 参数获取Token
     *
     * @param request
     * @return
     */
    public static String getQueryToken(HttpServletRequest request) {
        Map<String, String> paramMap = ServletUtil.getParamMap(request);
        return paramMap.get(AuthConstant.TOKEN_NAME);
    }
}
